<?php
include_once('./header.php');
?>
				<div id="corps-grand">
					<div id="tete-corps-grand"></div>
					<div id="milieu-corps-grand">
						<?php
						if((isset($_POST['valider']) AND isset($_POST['nom_personnage'])) OR (isset($_GET['personnage']) AND !empty($_GET['personnage']) AND ctype_alnum($_GET['personnage'])))
						{
							/*
							ctype_alnum — Vérifie qu'une chaîne est alphanumérique
							
							Fonctione avec:
							- Les noms (senssible à  la casse):
							  http://census.soe.com/get/ps2:v2/character/?name.first_lower=
							- les id:
							  http://census.soe.com/get/ps2:v2/character/?character_id=
							*/
							if(isset($_POST['valider']) AND isset($_POST['nom_personnage'])) {
								$nom = htmlspecialchars(strtolower($_POST['nom_personnage']));
							}
							if(isset($_GET['personnage']) AND !empty($_GET['personnage']) AND ctype_alnum($_GET['personnage'])) {
								$nom = htmlspecialchars(strtolower($_GET['personnage']));
							}
							
							$page = 'http://census.soe.com/get/ps2:v2/character/?name.first_lower=' . $nom . '&c:resolve=item_full&c:lang=fr';
							$census = file_get_contents($page);
							$parsed_json = json_decode($census);
							
/*
http://census.soe.com/get/ps2/item_type?c:limit=100&c:resolve=item_type


http://census.soe.com/get/ps2:v2/item_category?c:limit=1000&c:lang=fr

http://census.soe.com/get/ps2:v2/item?item_type_id=26&c:limit=1000&c:lang=fr

*/
							
							echo '<h2>Liste des '. $parsed_json->{'character_list'}[0]->{'title_id'} .' items de '. $parsed_json->{'character_list'}[0]->{'name'}->{'first'} .'</h2>';
							
							$parsed_json_true = json_decode($census,true);
							array_pop($parsed_json_true);
							
							echo '<table>
								<tr>
								<th>Items</th>
								<th>Descriptions</th>
								</tr>';
							foreach($parsed_json_true as $characters) {
								foreach($characters as $character) {
									$character_nom = $character['name'];
									$character_rang = $character['battle_rank'];
									$character_certs = $character['certs'];
									$character_charId = $character['character_id'];
									$character_rubansJournalier = $character['daily_ribbon'];
									$character_factionId = $character['faction_id'];
									$character_teteId = $character['head_id'];
									$character_items = $character['items'];
									$character_profileId = $character['profile_id'];
									$character_infoTemps = $character['times'];
									$character_titreID = $character['title_id'];

									//echo $character_nom['first'].'<br><br>';
								}
								foreach($character_items as $item) {
									$item_nom = $item['name'];
									$item_description = $item['description'];
									$image_id = $item['image_id'];
									$item_id = $item['item_id'];
									echo '<tr>
											<td><img title="' . $item_nom['fr'] . '" src="https://census.soe.com/files/ps2/images/static/' . $image_id . '.png" style="height:64px;" alt="' . $item_nom . '" /></td><td>' . $item_description['fr'] . '</td>
										</tr>';
								}
							}
							echo '</table>';
						}
						else {
							echo '<form name="personnage" action="./item.php" method="post">
								<p>Entrez le nom de votre personnage: <input type="text" name="nom_personnage" /> <input type="submit" name="valider" value="OK"/></p>
							</form>';
						}
						?>
					</div>
					<div id="pied-corps-grand"></div>
				</div>
				<br class="clear" /> 
<?php
include_once('./footer.php');
?>